package ddb.tasktracker.service.context.impl;

import java.io.File;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ddb.tasktracker.intf.config.dao.IConfigDao;
import ddb.tasktracker.intf.context.dto.ContextStatus;
import ddb.tasktracker.intf.context.service.IContextService;
import ddb.tasktracker.intf.shared.ErrorCode;
import tasktracker.core.framework.TaskTrackerContext;
import tasktracker.core.framework.TrackerNotFoundException;
import tasktracker.core.framework.WaitingQueueFullException;
import util.lang.RefValue;

@Service
public class ContextService implements IContextService {

	private static int contextStatus = ContextStatus.WORKING;

	@Autowired
	TaskTrackerContext context;

	@Autowired
	IConfigDao config;

	@Override
	public int getContextStatus() {
		return ContextService.contextStatus;
	}

	@Override
	public void setContextStatus(int contextStatus) {
		ContextService.contextStatus = contextStatus;
	}

	@Override
	public String newTask(String name, String trackerName, String owner, Map<String, String> properties, RefValue<Integer> errorCode) {
		String guid = UUID.randomUUID().toString();
		try {
			context.newTask(guid, name, trackerName, owner, getInputPath(guid), getOutputPath(guid), properties);
		} catch (TrackerNotFoundException e) {
			if (errorCode != null) {
				errorCode.setValue(ErrorCode.TRACKER_NOT_FOUND);
			}
			return null;
		}
		return guid;
	}

	@Override
	public boolean commitTask(String guid, RefValue<Integer> errorCode) {
		try {
			context.commitTask(guid);
		} catch (WaitingQueueFullException e) {
			if (errorCode != null) {
				errorCode.setValue(ErrorCode.WAITING_QUEUE_FULL);
			}
			return false;
		}
		return true;
	}

	@Override
	public File acquireTaskInputFileStore(String guid, boolean autoCreate) {
		String path = config.getFileStoreBasePath();
		File basePath = new File(path).getAbsoluteFile();

		if (!basePath.exists())
			basePath.mkdirs();

		File taskFileStore = new File(basePath, guid);
		File taskInputFileStore = new File(taskFileStore, "input");
		if (autoCreate)
			taskInputFileStore.mkdirs();

		return taskInputFileStore;
	}

	@Override
	public File acquireTaskOutputFileStore(String guid, boolean autoCreate) {
		String path = config.getFileStoreBasePath();
		File basePath = new File(path).getAbsoluteFile();

		if (!basePath.exists())
			basePath.mkdirs();

		File taskFileStore = new File(basePath, guid);
		File taskInputFileStore = new File(taskFileStore, "output");
		if (autoCreate)
			taskInputFileStore.mkdirs();

		return taskInputFileStore;
	}

	private String getInputPath(String guid) {
		String path = config.getFileStoreBasePath();
		File basePath = new File(path).getAbsoluteFile();
		File taskFileStore = new File(basePath, guid);
		File taskInputFileStore = new File(taskFileStore, "input");
		return taskInputFileStore.getAbsolutePath();
	}

	private String getOutputPath(String guid) {
		String path = config.getFileStoreBasePath();
		File basePath = new File(path);
		File taskFileStore = new File(basePath, guid);
		File taskInputFileStore = new File(taskFileStore, "output");
		return taskInputFileStore.getAbsolutePath();
	}
}
